package com.css.fxfzcpxx.modules.infoRetrieval.historicalSeismicInfo.service.impl;

import cn.hutool.core.util.IdUtil;
import com.css.fxfzcpxx.constants.DownloadEnum;
import com.css.fxfzcpxx.modules.infoRetrieval.historicalSeismicInfo.entity.JcEarthquakeDamageSurvey;
import com.css.fxfzcpxx.modules.infoRetrieval.historicalSeismicInfo.param.EarthquakeDisasterInfoParam;
import com.css.fxfzcpxx.modules.infoRetrieval.historicalSeismicInfo.param.HistoricalSeismicEventParam;
import com.css.fxfzcpxx.modules.infoRetrieval.historicalSeismicInfo.service.EarthquakeDisasterInfoService;
import com.css.fxfzcpxx.modules.util.JCSJLszhResp;
import com.css.fxfzcpxx.util.EasyPoiExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.IOUtils;
import org.slw.common.helper.PropsHelper;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/04/08
 * @Description: 地震灾害信息
 */
@Slf4j
@Service
public class EarthquakeDisasterInfoServiceImpl implements EarthquakeDisasterInfoService {
    @Resource
    private RestTemplate restTemplate;

    //分页
    @Override
    public Map<String, Object> getPage(int curPage, int pageSize, EarthquakeDisasterInfoParam param) {
        JCSJLszhResp<Map<String, Object>> pag = getPag(curPage, pageSize, param);
        Map<String, Object> data = pag.getData();
        return data;
    }

    //目录下载
    @Override
    public void exportData(HttpServletResponse response, String ids) {
        JCSJLszhResp<List<JcEarthquakeDamageSurvey>> queryByIds = getQueryByIds(ids);
        List<JcEarthquakeDamageSurvey> data = queryByIds.getData();
        EasyPoiExcelUtil.exportExcel(data, "地震灾害信息目录", "sheet1", JcEarthquakeDamageSurvey.class, "地震灾害信息目录", response);
    }

    //根据id查询
    @Override
    public String findById(String id) {
        return getFindById(id);
    }

    //获取基础数据信息
    private JCSJLszhResp<Map<String, Object>> getPag(int lon, int lat, EarthquakeDisasterInfoParam par){
        MultiValueMap<String, Object> param = new LinkedMultiValueMap();
        String mapping="/datacenter/jcEarthquakeDamageSurvey/getPage?lon=" + lon
                + "&lat=" + lat
                + "&startTime=" + par.getStartTime()
                + "&endTime=" + par.getEndTime()
                + "&startMagnitude=" + par.getStartMagnitude()
                + "&endMagnitude=" + par.getEndMagnitude()
                + "&longitude=" + par.getLongitude()
                + "&latitude=" + par.getLatitude()
                + "&province=" + par.getProvince()
                + "&city=" + par.getCity()
                + "&district=" + par.getDistrict()
                + "&town=" + par.getTown();
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("sjxtRestUrl") + mapping;
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.set("Authorization", IdUtil.simpleUUID());
        HttpEntity<MultiValueMap> r = new HttpEntity<>(param, headers);
        JCSJLszhResp<Map<String, Object>> response = null;
        try {
            response = restTemplate.exchange(url, HttpMethod.GET, r, JCSJLszhResp.class).getBody();
        } catch (
                RestClientException e) {
            log.info("error -> url: {}", url);
            log.info("error -> param: {}", param);
            log.info("error -> exception: {}", e);
        }
        return response;
    }
    //目录下载
    private JCSJLszhResp<List<JcEarthquakeDamageSurvey>> getQueryByIds(String ids){
        MultiValueMap<String, Object> param = new LinkedMultiValueMap();
        String mapping="/datacenter/jcEarthquakeDamageSurvey/exportData/" + ids;
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("sjxtRestUrl") + mapping;
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.set("Authorization", IdUtil.simpleUUID());
        HttpEntity<MultiValueMap> r = new HttpEntity<>(param, headers);
        JCSJLszhResp<List<JcEarthquakeDamageSurvey>> response = null;
        try {
            response = restTemplate.exchange(url, HttpMethod.POST, r, JCSJLszhResp.class).getBody();
        } catch (
                RestClientException e) {
            log.info("error -> url: {}", url);
            log.info("error -> param: {}", param);
            log.info("error -> exception: {}", e);
        }
        return response;
    }
    //根据id查询
    private String getFindById(String id){
        MultiValueMap<String, Object> param = new LinkedMultiValueMap();
        String mapping="/datacenter/jcEarthquakeDamageSurvey/getFindById/" + id;
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String url = configGroup.getProperty("sjxtRestUrl") + mapping;
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        headers.set("Authorization", IdUtil.simpleUUID());
        HttpEntity<MultiValueMap> r = new HttpEntity<>(param, headers);
        String response = null;
        try {
            response = restTemplate.exchange(url, HttpMethod.GET, r, String.class).getBody();
        } catch (
                RestClientException e) {
            log.info("error -> url: {}", url);
            log.info("error -> param: {}", param);
            log.info("error -> exception: {}", e);
        }
        return response;
    }
}
